package cn.changein.framework.util;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.MDC;

import java.util.UUID;

public class TraceLogUtils {
//    private static final ThreadLocal<String> traceIdLocal = new ThreadLocal();

    public TraceLogUtils() {
    }

    public static String getTraceId() {
        String traceId = MDC.get("traceId");
        if (StringUtils.isBlank(traceId)) {
            traceId = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 10);
            MDC.put("traceId", traceId);
        }
        return traceId;
    }

    public static void remove() {
        MDC.clear();
    }

    public static void set(String traceId) {
        MDC.put("traceId", traceId);
    }

}